class Centro::PersonalController < ApplicationController
  # GETs should be safe (see http://www.w3.org/2001/tag/doc/whenToUseGet.html)
  verify :method => :post, :only => [ :destroy, :create, :update ],
    :redirect_to => { :action => :list }
  
  def list
    conditions = {}
    conditions[:centro_id] = params[:id]
    
    if perfil_admin?
      @instances = Empleado.paginate :page => params[:page], :per_page => DEFAULT_PER_PAGE, :order => 'nombre', :conditions => conditions
    else
      centro = Centro.find(params[:id])
      
      @instances = centro.empleados.paginate :page => params[:page], :per_page => DEFAULT_PER_PAGE, :order => 'nombre'
    end
  end

  def show
    @empleado = Empleado.find(params[:id])
    
  end
  
  def new
    @empleado = Empleado.new
    @id = params[:id]
    
  end
  
  def create
    @empleado = Empleado.new(params[:empleado])
    @empleado.status_id = 1
    
    if @empleado.save
      flash[:notice] = "El Empleado se agrego correctamente."
      redirect_to :action => "list", :id => @empleado.centro_id
    else
      render :action => "new"
    end
    
  end
  
  def index
    redirect_to :controller => '/centro', :action => "list"
  end
  
  def edit
    @empleado = Empleado.find(params[:id])
    
  end
  
  def update
    @empleado = Empleado.find(params[:id])
    
    if @empleado.update_attributes(params[:empleado])
      flash[:notice] = "El Empleado se actualizo correctamente."
      redirect_to :action => "show", :id => @empleado
    else
      redirect_to :action => "edit"
    end
  end
  
  def deshabilitar
    empleado = Empleado.find(params[:id])
    
    empleado.status_id = 0
    
    if empleado.save
      flash[:notice] = "Empleado deshabilitado..."
    end
    
    redirect_to :action => "index"
  end
end
